﻿@using BlueLotus7.Eden.KB.Status.Models;
@using BlueLotus7.Eden.KB.Models;
@using BlueLotus7.Web;
@model StatusQueryPageModel
@{
    ViewBag.Title = "Index";
    Layout = BlueLotus7.Eden.Web.Helper.MasterPageUrl;
}
@section TitleContent{
Query
}
@section MainContent{
    <div>
        <h2>
            Query</h2>
        <div id="queryPanel">
            <div class="line">
                <div id="queryPanelDomainScopes" class="criteria">
                    <label>
                        Domain Scope:
                    </label>
                    @Html.Partial("ControlView/DomainScopeListControlView", Model as DomainScopeListControlModel)
                </div>
                <div id="queryPanelDomainScopeUsers" class="criteria">
                    <label>
                        Domain Scope User:
                    </label>
                    <select id="domainScopeUserList">
                    </select>
                </div>
            </div>
            <div class="line">
                <div id="queryPanelStartDate" class="criteria">
                    <label>
                        From:
                    </label>
                    <input type="date" id="fromDate" />
                </div>
                <div id="queryPanelEndDate" class="criteria">
                    <label>
                        To:
                    </label>
                    <input type="date" id="toDate" />
                </div>
                <div id="queryPanelDisplayView" class="criteria">
                    <label>
                        View:
                    </label>
                    @{
                        StatusViewControlModel model = new StatusViewControlModel();
                        Html.RenderPartial("ControlView/StatusViewControlView", model); 
                    }
                </div>
                <a class="button btn-query">Query</a>
            </div>
        </div>
        <div id="queryResult">
        </div>
        <div id="statusMergePanel">
            <div id="mergePanelStatusValue" class="criteria">
                <label>
                    Merge to status:
                </label>
                <select id="statusList">
                </select>
            </div>
            <a class="button btn-merge">Merge Status</a>
        </div>
    </div>
}
@section ResourceReferences{
    @{
        List<GridSource> gridSources = new List<GridSource>();
        foreach (var one in Model.DomainScopes)
        {
            Type type = BlueLotus7.Eden.Web.Helper.GetStatusEnumTypeByCode(one.Code);
            if (type != null)
            {

                GridSource source = new GridSource(one.Key.ToString());
                foreach (var enumItem in Enum.GetValues(type))
                {
                    source.Items.Add(new KeyValuePair<string, string>(Convert.ToInt32(enumItem).ToString(), enumItem.ToString()));
                }
                gridSources.Add(source);
            }
        }
        System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        string statusSources = serializer.Serialize(gridSources);        
    }
    <script type="text/javascript">
        dataSource.tryInit();
        dataSource.addSourceRange(@(Html.Raw(statusSources)));
    </script>
    <script type="text/javascript">
        var statusGridContainer = undefined;
        var queryPanel = undefined;


        $(document).ready(function () {
            queryPanel = $("#queryPanel");
            statusGridContainer = $("#queryResult");
            bindDomainScopesListEvent();
            var dates = queryPanel.find("input[type='date']").attr("readonly", true).bind("keyup", {}, function (e) {
                if (e.keyCode == 8 || e.keyCode == 46) {
                    $(this).val("");
                }
                return false;
            }).datepicker({
                dateFormat: "yy-mm-dd",
                changeMonth: true,
                changeYear: true,
                numberOfMonths: 3,
                onSelect: function (selectedDate) {
                    var option = this.id == "fromDate" ? "minDate" : "maxDate",
					    instance = $(this).data("datepicker"),
					    date = $.datepicker.parseDate(
						    instance.settings.dateFormat ||
						    $.datepicker._defaults.dateFormat,
						    selectedDate, instance.settings);
                    dates.not(this).datepicker("option", option, date);
                }
            });

            $("#queryPanel .btn-query").bind("click", {}, function (e) {
                queryStatusByCriterias(queryPanel);
            });

            $("#statusMergePanel .btn-merge").bind("click", {}, function (e) {
                var status = $("#statusList").val();
                mergeStatus(statusGridContainer.find("table:last"), status);
            });
        });
    </script>
}
@section SideNavigator{
}
